package com.concerneyes.rentbarsecond.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.concerneyes.CEApp;
import com.concerneyes.R;
import com.concerneyes.pay.DialogPay;
import com.concerneyes.rentbar.dao.ShoppingCartBean;
import com.concerneyes.rentbar.listener.OnShoppingCartChangeListener;
import com.concerneyes.rentbar.util.ShoppingCartBiz;
import com.concerneyes.rentbarsecond.db.GlassesDetailModelForDao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import util.ImageUtil;
import util.ToastHelper;

/**
 * Created by Administrator on 2017/6/19 0019.
 */

public class NewRentBarAdapter extends BaseAdapter implements View.OnClickListener {
    Context mContext;
    private OnPayListener onPayListener;
    List<GlassesDetailModelForDao> daos;
    private boolean isVisible;
    private boolean isClick;
    private boolean isClickAll;
    private HashMap<Integer, Boolean> itemClickMap;

    public NewRentBarAdapter(Context mContext, OnPayListener onPayListener, List<GlassesDetailModelForDao> daos) {
        this.mContext = mContext;
        this.onPayListener = onPayListener;
        this.daos = daos;
        itemClickMap = new HashMap<>();

        for (int i = 0; i < daos.size(); i++) {
            itemClickMap.put(i, false);
        }
    }

    public interface OnPayListener {
        void payClick(View v);
    }

    public void clear() {
        daos.clear();
        notifyDataSetChanged();
    }

    public void setVisible(boolean isVisible) {
        this.isVisible = isVisible;
        notifyDataSetChanged();
    }

    public void setClick(HashMap<Integer, Boolean> itemClickMap) {
        this.itemClickMap = itemClickMap;
        notifyDataSetChanged();
    }

    public void setClickAll(boolean isClickAll) {
        this.isClickAll = isClickAll;
        notifyDataSetChanged();
    }

    public void addItems(List<GlassesDetailModelForDao> daos) {
        if (daos != null && daos.size() > 0) {
            this.daos.addAll(daos);
        }
        notifyDataSetChanged();
    }

    @Override
    public GlassesDetailModelForDao getItem(int position) {
        return daos.get(position);
    }

    @Override
    public int getCount() {
        return daos.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        viewHolder holder = null;
        if (convertView == null) {
            holder = new viewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_rentbar_new_view, null);
            holder.tvItemtitle = (TextView) convertView.findViewById(R.id.tvItemtitle);
            holder.tvGoodsParam = (TextView) convertView.findViewById(R.id.tvGoodsParam);
            holder.tvGoodsParam1 = (TextView) convertView.findViewById(R.id.tvGoodsParam1);
            holder.tvPrice = (TextView) convertView.findViewById(R.id.tvPrice);
            holder.payNow = (TextView) convertView.findViewById(R.id.pay_now);
            holder.ivGoods = (ImageView) convertView.findViewById(R.id.ivGoods);
            holder.iv_select = (ImageView) convertView.findViewById(R.id.iv_select);
            convertView.setTag(holder);
        } else {
            holder = (viewHolder) convertView.getTag();
        }

        holder.tvItemtitle.setText(daos.get(position).getTitle());
        holder.tvGoodsParam.setText("颜色/" + daos.get(position).getFrameColor());
        holder.tvGoodsParam1.setText("镜片/" + daos.get(position).getRefractivetitle());
        holder.tvPrice.setText(daos.get(position).getTotalPrice());
        ImageUtil.glideloadImage(CEApp.getResLink(daos.get(position).getImgPath()), holder.ivGoods, R.drawable.default_header);

        if (isVisible) {
            holder.iv_select.setVisibility(View.VISIBLE);
            holder.iv_select.setOnClickListener(this);
            holder.iv_select.setTag(position);
            Log.e(this.getClass().getSimpleName(), "itemClickMap==>" + itemClickMap);
            if (itemClickMap.containsKey(position)) {
                if (itemClickMap.get(position)) {
                    holder.iv_select.setImageDrawable(mContext.getResources().getDrawable(R.drawable.btn_fx_selected));
                } else {
                    holder.iv_select.setImageDrawable(mContext.getResources().getDrawable(R.drawable.btn_fx_unselected));
                }
            } else {
                holder.iv_select.setImageDrawable(mContext.getResources().getDrawable(R.drawable.btn_fx_unselected));
            }
        } else {
            holder.iv_select.setVisibility(View.GONE);
        }

        holder.payNow.setOnClickListener(this);
        holder.payNow.setTag(position);

        return convertView;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.pay_now:
                onPayListener.payClick(v);
                break;
            case R.id.iv_select:
                onPayListener.payClick(v);
                break;
        }
    }

    class viewHolder {
        TextView tvItemtitle;
        TextView tvGoodsParam;
        TextView tvGoodsParam1;
        TextView tvPrice;
        TextView payNow;
        ImageView ivGoods;
        ImageView iv_select;

    }
}
